package com.lin.fengxiu.api.v1;

import com.lin.fengxiu.core.LocalUser;
import com.lin.fengxiu.core.UnifyResponse;
import com.lin.fengxiu.core.interceptors.ScopeLevel;
import com.lin.fengxiu.exception.CreateSuccess;
import com.lin.fengxiu.model.Coupon;
import com.lin.fengxiu.model.User;
import com.lin.fengxiu.service.CouponService;
import com.lin.fengxiu.vo.CouponPureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName CouponController
 * @Description TODO
 * @Author wushaopei
 * @Date 2021/3/24 22:08
 * @Version 1.0
 */
@RequestMapping("coupon")
@RestController
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * @Description TODO 根据分类id查询所属优惠券
     * @param cid
     * @return
     */
    @GetMapping("/by/category/{cid}")
    public List<CouponPureVO> getCouponListByCategory(
            @PathVariable Long cid){
        // 传入category，优惠券默认属于二级分类，所以cid为二级分类所有
        List<Coupon> coupons = couponService.getByCategory(cid);
        if(coupons.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponPureVO> vos = CouponPureVO.getList(coupons);
        return vos;
    }

    /**
     * @Description TODO 全场券
     * @return
     */
    @GetMapping("/whole_store")
    public List<CouponPureVO> getWholeStoreCouponList(){
        // 传入category，优惠券默认属于二级分类，所以cid为二级分类所有
        List<Coupon> coupons = couponService.getWholeStoreCoupons();
        if(coupons.isEmpty()){
            return Collections.emptyList();
        }
        List<CouponPureVO> vos = CouponPureVO.getList(coupons);
        return vos;
    }

    /**
     * @Description TODO 优惠券领取
     * @param id
     */
    @ScopeLevel()
    @PostMapping("/collect/{id}")
    public void coolectCoupon(@PathVariable Long id){
        Long uid = LocalUser.getUser().getId();
        System.out.println("-------------");
        couponService.collectOneCoupon(uid, id);
        UnifyResponse.createSuccess(0);
    }
}
